package genedit.model.graph.classModel;


import genedit.gui.GMainFrame;
import genedit.gui.exceptions.ElementNePostoji;
import genedit.gui.exceptions.ElementPostoji;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;





public class GAttributes implements ITableModel, Serializable{

		/**
	 * 
	 */
	private static final long serialVersionUID = -7662663253808627865L;
		private Map<String, GAttribute> attributes = new HashMap<String, GAttribute>();
		private Map<String, GAttribute> inheritedAttributes = new HashMap<String, GAttribute>();
		
		public void addAtribute(GAttribute novi) throws ElementPostoji {
			if (attributes.containsKey(novi.getName()))
				throw new ElementPostoji(GMainFrame.getInstance().getResourceBundle().getString("PostojiA"));
			attributes.put(novi.getName(), novi);
		}
		public HashMap<String, GAttribute> getAttributes() {
			return (HashMap<String, GAttribute>)attributes;
		}
		
		public void changeAtribute(GAttribute novi) throws ElementNePostoji {
			if (!attributes.containsKey(novi.getName()))
				throw new ElementNePostoji(GMainFrame.getInstance().getResourceBundle().getString("NePostojiA"));
			attributes.put(novi.getName(), novi);
		}
		
		public void removeAtribute(GAttribute stari) throws ElementNePostoji {
			if (!attributes.containsKey(stari.getName()))
				throw new ElementNePostoji(GMainFrame.getInstance().getResourceBundle().getString("NePostojiA"));
			attributes.remove(stari.getName());
		}
		
		public GAttribute getAtribute(String key) throws ElementNePostoji {
			if (!attributes.containsKey(key))
				throw new ElementNePostoji(GMainFrame.getInstance().getResourceBundle().getString("NePostojiA"));
			return attributes.get(key);
		}
		
		public Iterator<String> getAtributeIterator() {
			Set<String> names = attributes.keySet();
			return names.iterator();
		}
		
		public Set<String> getAtributeKeys() {
			Set<String> names = attributes.keySet();
			return names;
		}

		public Map<String, GAttribute> getInheritedAttributes() {
			return inheritedAttributes;
		}
		public void setInheritedAttributes(Map<String, GAttribute> inheritedAttributes) {
			this.inheritedAttributes = inheritedAttributes;
		}
		@Override
		public int getRowCount() {
			return attributes.size();
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			ArrayList<GAttribute> atributi = new ArrayList<GAttribute>(attributes.values());
			switch(columnIndex){
				case 0: return atributi.get(rowIndex).getVisibility();
				case 1: return atributi.get(rowIndex).getType();
				case 2: return atributi.get(rowIndex).getName();
				case 3: return atributi.get(rowIndex).getPreDef();
				case 4: return atributi.get(rowIndex).isGetAttribute();
				case 5: return atributi.get(rowIndex).isSetAttribute();
			}
			return "";
		}
	}
